package cn.tqfeiyang.smartadmin.office.controller;

import cn.tqfeiyang.smartadmin.office.service.NoticeService;
import cn.tqfeiyang.smartadmin.office.request.NoticeCreateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeUpdateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeQueryDTO;
import cn.tqfeiyang.smartadmin.office.response.NoticeQueryVO;
import cn.tqfeiyang.smartadmin.office.response.NoticeDetailVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.Set;

/**
 * 通知控制器
 *
 * @author tqfeiyang
 * @since 2024-09-12
 */
@RestController
@Validated
@RequestMapping("/api/office/notice")
@Tag(name = "办公/通知")
public class NoticeController {

    /**
     * 通知服务
     */
    @Resource
    private NoticeService noticeService;

    /**
     * 分页查询通知
     *
     * @param dto 请求数据
     * @return 通知分页
     */
    @OperationLog
    @GetMapping("/getNoticePage")
    @Operation(summary = "通知 - 分页查询")
    @SaCheckPermission("office:notice:getNoticePage")
    public ApiResult<PageData<NoticeQueryVO>> getNoticePage(@Validated NoticeQueryDTO dto) {
        return ApiResult.success(noticeService.getNoticePage(dto));
    }

    /**
     * 获取全部通知
     *
     * @return 通知列表
     */
    @OperationLog
    @GetMapping("/getAllNoticeList")
    @Operation(summary = "通知 - 获取全部")
    @SaCheckPermission("office:notice:getAllNoticeList")
    public ApiResult<NoticeQueryVO> getAllNoticeList() {
        return ApiResult.success(noticeService.getAllNoticeList());
    }

    /**
     * 获取通知详情
     *
     * @param noticeId 通知id
     * @return 通知详情
     */
    @OperationLog
    @GetMapping("/getNoticeInfo/{noticeId}")
    @Operation(summary = "通知 - 获取详情")
    @SaCheckPermission("office:notice:getNoticeInfo")
    public ApiResult<NoticeDetailVO> getNoticeInfo(@PathVariable @EntityId Long noticeId) {
        return ApiResult.success(noticeService.getNoticeInfo(noticeId));
    }

    /**
     * 添加通知
     *
     * @param dto 请求数据
     * @return 通知详情
     */
    @OperationLog
    @PostMapping("/createNotice")
    @Operation(summary = "通知 - 添加")
    @SaCheckPermission("office:notice:createNotice")
    public ApiResult<NoticeDetailVO> createNotice(@RequestBody @Validated NoticeCreateDTO dto) {
        Long noticeId = noticeService.createNotice(dto);
        return ApiResult.success(noticeService.getNoticeInfo(noticeId));
    }

    /**
     * 修改通知
     *
     * @param noticeId 通知id
     * @param dto 请求数据
     * @return 通知详情
     */
    @OperationLog
    @PostMapping("/updateNotice/{noticeId}")
    @Operation(summary = "通知 - 修改")
    @SaCheckPermission("office:notice:updateNotice")
    public ApiResult<NoticeDetailVO> updateNotice(@PathVariable @EntityId Long noticeId, @RequestBody @Validated NoticeUpdateDTO dto) {
        noticeService.updateNotice(noticeId, dto);
        return ApiResult.success(noticeService.getNoticeInfo(noticeId));
    }

    /**
     * 删除通知
     *
     * @param noticeId 通知id
     */
    @OperationLog
    @PostMapping("/deleteNotice/{noticeId}")
    @Operation(summary = "通知 - 删除")
    @SaCheckPermission("office:notice:deleteNotice")
    public ApiResult deleteNotice(@PathVariable @EntityId Long noticeId) {
        noticeService.deleteNotice(noticeId);
        return ApiResult.success();
    }

    /**
     * 批量删除通知
     *
     * @param noticeIds 通知id集合
     */
    @OperationLog
    @PostMapping("/batchDeleteNotices")
    @Operation(summary = "通知 - 批量删除")
    @SaCheckPermission("office:notice:batchDeleteNotices")
    public ApiResult batchDeleteNotices(@RequestBody Set<Long> noticeIds) {
        noticeService.batchDeleteNotices(noticeIds);
        return ApiResult.success();
    }

    /**
     * 导出Excel
     *
     * @param response 响应对象
     */
    @OperationLog
    @GetMapping("/exportNoticeExcel")
    @Operation(summary = "通知 - 导出Excel")
    @SaCheckPermission("office:notice:exportNoticeExcel")
    public void exportNoticeExcel(HttpServletResponse response) {
        noticeService.exportNoticeExcel(response);
    }

    /**
     * 修改排序
     *
     * @param noticeId 通知id
     * @param sort 排序
     */
    @OperationLog
    @PostMapping("/updateSortField/{noticeId}/{sort}")
    @Operation(summary = "通知 - 修改排序")
    @SaCheckPermission("office:notice:updateSortField")
    public ApiResult updateSortField(@PathVariable @EntityId Long noticeId, @PathVariable Integer sort) {
        noticeService.updateSortField(noticeId, sort);
        return ApiResult.success();
    }
}
